/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
		
/*----------------------------Head file----------------------------*/
#include "rwip_config.h"     // SW configuration
#include <string.h>
#include "app_ota.h"                // Bracese Application Module Definitions
#include "app.h"                     // Application Definitions
#include "app_task.h"                // application task definitions
#include "ota.h"
#include "ota_task.h"               // health thermometer functions
#include "co_bt.h"
#include "prf_types.h"               // Profile common types definition
#include "arch.h"                    // Platform Definitions
#include "prf.h"
#include "lld_evt.h"

/*----------------------------macro file---------------------------*/
#define APP_OTA_LOG_ENABLE		0

#if APP_OTA_LOG_ENABLE
#include "log.h"
#define APP_OTA_LOG				LOG_DEBUG
#else
#define APP_OTA_LOG(...)
#endif

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
/// OTA Application Module Environment Structure
struct app_ota_env_tag app_ota_env;

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

void app_ota_init(void)
{
    // Reset the environment
    memset(&app_ota_env, 0, sizeof(struct app_ota_env_tag));
}


void app_ota_add(void)
{
	struct ota_db_cfg *db_cfg;
	struct gapm_profile_task_add_cmd *req = KE_MSG_ALLOC_DYN(GAPM_PROFILE_TASK_ADD_CMD,
                                                  TASK_GAPM, TASK_APP,
                                                  gapm_profile_task_add_cmd, sizeof(struct ota_db_cfg));
    // Fill message
    req->operation = GAPM_PROFILE_TASK_ADD;
    req->sec_lvl = 0x40; //PERM_MASK_SVC_UUID_LEN 128 bit
    req->prf_task_id = TASK_ID_OTA;
    req->app_task = TASK_APP;
    req->start_hdl = 0; //dynamically allocated

	  // Set parameters
    db_cfg = (struct ota_db_cfg* ) req->param;
	 
    // Sending of notifications is supported
    db_cfg->features = OTA_NTF_SUP;
	//APP_OTA_LOG("app_ota_add d = %x,s = %x\r\n", TASK_GAPM, TASK_APP);
    // Send the message
    ke_msg_send(req);
}


/**
 ****************************************************************************************
 * @brief
 *
 * @param[in] msgid     Id of the message received.
 * @param[in] param     Pointer to the parameters of the message.
 * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
 * @param[in] src_id    ID of the sending task instance.
 *
 * @return If the message was consumed or not.
 ****************************************************************************************
 */
static int app_ota_msg_dflt_handler(ke_msg_id_t const msgid,
                                     void const *param,
                                     ke_task_id_t const dest_id,
                                     ke_task_id_t const src_id)
{
    // Drop the message
	//APP_OTA_LOG("%s\r\n",__func__);
	//APP_OTA_LOG("msgid = 0x%04x,destid = 0x%x,srcid = 0x%x\r\n",msgid, dest_id, src_id);
    return (KE_MSG_CONSUMED);
}

/*
 * LOCAL VARIABLE DEFINITIONS
 ****************************************************************************************
 */

/// Default State handlers definition
const struct ke_msg_handler app_ota_msg_handler_list[] =
{
    // Note: first message is latest message checked by kernel so default is put on top.
	{KE_MSG_DEFAULT_HANDLER,        (ke_msg_func_t)app_ota_msg_dflt_handler},
};

const struct ke_state_handler app_ota_table_handler =
    {&app_ota_msg_handler_list[0], (sizeof(app_ota_msg_handler_list)/sizeof(struct ke_msg_handler))};


